package com.sip.stream.utils.zfy.voice;

import android.content.Context;
import android.media.AudioAttributes;
import android.media.SoundPool;
import android.util.SparseArray;

import com.sip.stream.ClientApp;
import com.sip.stream.R;

import io.reactivex.Single;
import io.reactivex.SingleObserver;
import io.reactivex.disposables.Disposable;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

public abstract class AbstractBeepProvider {
    private SoundPool soundPool;
    private SparseArray<Integer> sampleIdMap = new SparseArray<>();
    protected int redBlueAlarmSoundId = -1;

    public void changeAudioAttributes(boolean z) {
    }

    public void changeLanguage(Locale locale) {
    }

    public abstract void play(String str, String str2);

    public void playTime(long j) {
    }

    public void release() {
        SoundPool soundPool = this.soundPool;
        if (soundPool != null) {
            soundPool.release();
            this.soundPool = null;
            this.sampleIdMap.clear();
        }
    }

    public void setUp(GSCallBack<Boolean> gSCallBack) {
        if (gSCallBack != null) {
            gSCallBack.onComplete(new GSResult<>());
        }
    }

    public int getBeepIdentifier(String str) {
        str.hashCode();
        char c = 65535;
        switch (str.hashCode()) {
            case -1103225498:
                if (str.equals(BeepType.VIDEO_STOP)) {
                    c = 0;
                    break;
                }
                break;
            case 732893662:
                if (str.equals(BeepType.CAPTURED)) {
                    c = 1;
                    break;
                }
                break;
            case 1267554278:
                if (str.equals(BeepType.DEVICE_OFF)) {
                    c = 2;
                    break;
                }
                break;
            case 1273035988:
                if (str.equals(BeepType.VOIP_CALL_FINISHED)) {
                    c = 3;
                    break;
                }
                break;
            case 1651886964:
                if (str.equals(BeepType.PRE_RECORD_STOP)) {
                    c = 4;
                    break;
                }
                break;
            case 2063988459:
                if (str.equals(BeepType.AUDIO_STOP)) {
                    c = 5;
                    break;
                }
                break;
        }
        switch (c) {
            case 0:
            case 2:
            case 3:
            case 4:
            case 5:
                return R.raw.common_didi;
            case 1:
                return R.raw.common_ka;
            default:
                return R.raw.common_ding;
        }
    }

    public Context getContext() {
        return ClientApp.clientApp.getApplicationContext();
    }

    protected SoundPool getSoundPool() {
        SoundPool soundPool = this.soundPool;
        if (soundPool != null) {
            return soundPool;
        }
        SoundPool build = new SoundPool.Builder().setMaxStreams(1).setAudioAttributes(new AudioAttributes.Builder().setLegacyStreamType(5).setContentType(4).setUsage(5).build()).build();
        this.soundPool = build;
        build.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
            @Override
            public  void onLoadComplete(SoundPool soundPool2, int i, int i2) {
                AbstractBeepProvider.this.m308xe51c1070(soundPool2, i, i2);
            }
        });
        return this.soundPool;
    }

    public  void m308xe51c1070(SoundPool soundPool, int i, int i2) {
        int i3 = this.redBlueAlarmSoundId;
        if (i3 > 0 && i == i3) {
            soundPool.play(i, 1.0f, 1.0f, 1, -1, 1.0f);
        } else {
            soundPool.play(i, 1.0f, 1.0f, 1, 0, 1.0f);
        }
    }

    public void loadOrPlaySoundPool(int i) {
        if (i < 0) {
            return;
        }
        Integer num = this.sampleIdMap.get(i);
        if (num == null) {
            this.sampleIdMap.put(i, Integer.valueOf(getSoundPool().load(ClientApp.clientApp, i, 1)));
            return;
        }
        this.soundPool.play(num.intValue(), 1.0f, 1.0f, 1, 0, 1.0f);
    }

    public synchronized void playRedBlueAlarm(boolean z) {
        if (z) {
            if (this.redBlueAlarmSoundId < 0) {
                this.redBlueAlarmSoundId = getSoundPool().load(ClientApp.clientApp, R.raw.redblue_alarm, 1);
            }
        } else if (this.redBlueAlarmSoundId > 0) {
            getSoundPool().stop(this.redBlueAlarmSoundId);
            getSoundPool().unload(this.redBlueAlarmSoundId);
            this.redBlueAlarmSoundId = -1;
        }
    }

    public void autoPauseAlarm() {
        SoundPool soundPool = this.soundPool;
        if (soundPool != null) {
            soundPool.autoPause();
            Single.just(Boolean.TRUE).delay(3L, TimeUnit.SECONDS).subscribe(new SingleObserver<Boolean>() {
                @Override
                public void onError(Throwable th) {
                }

                @Override
                public void onSubscribe(Disposable disposable) {
                }

                @Override
                public void onSuccess(Boolean bool) {
                    AbstractBeepProvider.this.soundPool.autoResume();
                }
            });
        }
    }

    public void beepOneShot() {
        loadOrPlaySoundPool(R.raw.common_ding);
    }
}
